package com.ccx.web.front;


import com.ccx.model.EbProduct;
import com.ccx.model.EbProductCategory;
import com.ccx.service.EbProductCategoryService;
import com.ccx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/front")
public class FrontController {

    @Autowired
    private ProductService productService;

    @Autowired
    private EbProductCategoryService productCategoryService;
    /**
     * 显示首页
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model,
                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "pageSize",defaultValue = "8") Integer pageSize){
        //1.分页的新闻数据


        //2.分页商品数据
        List<EbProduct> ebProducts = productService.list(page, pageSize);
        model.addAttribute("ebProducts",ebProducts);

        //3.商品分类数据
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigAndSmallCategory();
        model.addAttribute("categoryMap",categoryMap);
        //4.最近浏览的商品数据  [不做]

        //视图：  / index.jsp
        return "index";
    }

    //根据Id查询相对应的商品类型
    @RequestMapping("/edit")
    public String editProduct(Model model,int epId){
        EbProduct ebProduct = productService.selectbyId(epId);

        //绑定显示的商品对象
        model.addAttribute("ebProduct",ebProduct);
        //查询所有大类和小类
        //调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigAndSmallCategory();
        //通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);

        return "product-view";
    }
}
